package com.kongjian.manage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kongjian.manage.common.CustomException;
import com.kongjian.manage.common.R;
import com.kongjian.manage.mapper.UserMapper;
import com.kongjian.manage.model.dto.NewUserDTO;
import com.kongjian.manage.model.dto.UpdateUserDTO;
import com.kongjian.manage.model.entity.UserEntity;
import com.kongjian.manage.model.query.UserQuery;
import com.kongjian.manage.model.vo.UserVO;
import com.kongjian.manage.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author tangjunfeng
 * @Classname UserServiceImpl.java
 * @Description 用户service
 * @Date 2023-10-09
 * @Version V1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Override
    public R queryPageUser(UserQuery query) throws Exception {
        //构造分页构造器
        Page pageInfo = new Page(query.getPage(), query.getSize());
        LambdaQueryWrapper<UserEntity> lambda = queryWhere(query);

        //执行查询
        Page result = this.page(pageInfo, lambda);
        return R.success(result);
    }

    @Override
    public UserVO getByIdUser(Long id) throws Exception {
        UserVO userVO = new UserVO();
        UserEntity userEntity = baseMapper.selectById(id);
        if (userEntity != null) {
            BeanUtils.copyProperties(userEntity, userVO);
        }
        return userVO;
    }

    @Override
    public void addUser(NewUserDTO userDTO) throws Exception {
        UserEntity user = new UserEntity();
        BeanUtils.copyProperties(userDTO, user);
        baseMapper.insert(user);
    }

    @Override
    public void modifyUser(Long id, UpdateUserDTO userDTO) throws Exception {
        UserEntity user = baseMapper.selectById(id);
        if (user != null) {
            if (userDTO.getFUserId() != null) {
                user.setFUserId(userDTO.getFUserId());
            }
            if (userDTO.getFUserName() != null) {
                user.setFUserName(userDTO.getFUserName());
            }
            if (userDTO.getFUserCode() != null) {
                user.setFUserCode(userDTO.getFUserCode());
            }
            if (userDTO.getFUserTitle() != null) {
                user.setFUserTitle(userDTO.getFUserTitle());
            }
            if (userDTO.getFUserGuid() != null) {
                user.setFUserGuid(userDTO.getFUserGuid());
            }
            if (userDTO.getFUserSex() != null) {
                user.setFUserSex(userDTO.getFUserSex());
            }
            if (userDTO.getFUserUnit() != null) {
                user.setFUserUnit(userDTO.getFUserUnit());
            }
            if (userDTO.getFUserLastName() != null) {
                user.setFUserLastName(userDTO.getFUserLastName());
            }
            if (userDTO.getFUserFirstName() != null) {
                user.setFUserFirstName(userDTO.getFUserFirstName());
            }
            if (userDTO.getFUserCountry() != null) {
                user.setFUserCountry(userDTO.getFUserCountry());
            }
            if (userDTO.getLoggedInObject() != null) {
                user.setLoggedInObject(userDTO.getLoggedInObject());
            }
            baseMapper.updateById(user);
        }
    }

    @Override
    public void deleteUser(Long id) throws Exception {
        UserEntity userEntity = baseMapper.selectById(id);
        if (userEntity == null) {
            throw new CustomException("id:" + id + "没有查到数据");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public Integer countUser(UserQuery query) {
        LambdaQueryWrapper<UserEntity> lambda = queryWhere(query);
        return baseMapper.selectCount(lambda).intValue();
    }

    private LambdaQueryWrapper<UserEntity> queryWhere(UserQuery query) {
        LambdaQueryWrapper<UserEntity> lambda = new LambdaQueryWrapper<>();
        lambda.eq(query.getFUserId() != null, UserEntity::getFUserId, query.getFUserId());
        lambda.eq(query.getFUserName() != null, UserEntity::getFUserName, query.getFUserName());
        lambda.eq(query.getFUserCode() != null, UserEntity::getFUserCode, query.getFUserCode());
        lambda.eq(query.getFUserTitle() != null, UserEntity::getFUserTitle, query.getFUserTitle());
        lambda.eq(query.getFUserGuid() != null, UserEntity::getFUserGuid, query.getFUserGuid());
        lambda.eq(query.getFUserSex() != null, UserEntity::getFUserSex, query.getFUserSex());
        lambda.eq(query.getFUserUnit() != null, UserEntity::getFUserUnit, query.getFUserUnit());
        lambda.eq(query.getFUserLastName() != null, UserEntity::getFUserLastName, query.getFUserLastName());
        lambda.eq(query.getFUserFirstName() != null, UserEntity::getFUserFirstName, query.getFUserFirstName());
        lambda.eq(query.getFUserCountry() != null, UserEntity::getFUserCountry, query.getFUserCountry());
        lambda.eq(query.getLoggedInObject() != null, UserEntity::getLoggedInObject, query.getLoggedInObject());
        return lambda;
    }

}
